<?php
/**
 * Created by PhpStorm.
 * User: refar
 * Date: 22-5-26
 * Time: 下午4:16
 */

namespace app\v2\controller;

use think\Db;
use think\Exception;
use think\Log;
use think\Request;

class Attachment extends Common {
    /**
     * showdoc
     * @catalog 接口文档-V2/附件相关
     * @title 单文件上传
     * @description 单文件上传
     * @method post
     * @param attachment 必选 file 文件
     * @return file_id, file_name
     * @url http://domain/ems-api/v2/Attachment/upload
     * @remark 1. 需要post请求; 2. Content-Type=multipart/form-data
     */
    public function upload() {
        $attachment = request()->file('attachment')->getInfo();

        // 上传excel到服务器 (以当前日期为子目录，以微秒时间的md5编码为文件名的文件)
        $file = request()->file('attachment');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'attachments.v2');
            if ($info) {
                // 成功上传后 获取上传信息
                Log::record('[Attachment][upload] upload file ' . $attachment['name'] . ' | ' .
                        $info->getSaveName());
                $id = explode('.', $info->getFilename())[0];
                // 返回附件id
                $data = [
                        'id'         => $id,
                        'file_name'  => $info->getFilename(),
                        'path'       => ROOT_PATH . 'public' . DS . 'attachments.v2'. DS . $info->getSaveName(),
                        'upload_time'=> Db::raw('now()')
                ];
                $res = Db::table('sms_attachment')->insert($data);

                if ($res == 1) {
                    Log::record('[Attachment][insert] database success ' .
                        $attachment['name'] . ' | ' . $info->getSaveName());

                    $jsonResult = ['file_id' => $id, 'file_name' => $info->getFilename()];

                    return apiResponse(SUCCESS, '[Attachment][upload] upload success ', $jsonResult);

                } else {
                    Log::record('[Attachment][insert] database fail ' . $attachment['name'] .
                        ' | ' . $info->getSaveName());

                    return apiResponse(ERROR, 'server error');
                }

            } else {
                // 上传失败获取错误信息
                Log::record('[Attachment][upload] upload error ' . $file->getError());
                return apiResponse(ERROR, 'server error');
            }
        }
    }
    /**
     * showdoc
     * @catalog 接口文档-V2/附件相关
     * @title 图片回显
     * @description 图片回显
     * @method get
     * @param id 必选 int 附件id
     * @return 无
     * @url http://domain/ems-api/v2/Attachment/showAttachment
     * @remark
     */
    public function showAttachment() {
        $id = $this->request->param('id');

        try {
            $res = Db::table('sms_attachment')->where('id', $id)->find();

            if (empty($res)) {
                return apiResponse(SUCCESS, '[Attachment][showAttachment] no attachment');
            }
            $str=file_get_contents($res['path']);
            header("Content-Type: image/jpeg;text/html; charset=utf-8");
            echo $str;die;

        } catch (Exception $e) {
            Log::record('[Attachment][showAttachment] error ' . $e->getMessage());
            return apiResponse(ERROR, 'server error');
        }

    }
}

